package com.platform.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.annotation.Authentication;
import com.platform.base.PageUtil;
import com.platform.base.Tree;
import com.platform.dao.RightDAO;
import com.platform.dao.RoleRightDAO;
import com.platform.dto.Right;
import com.platform.dto.RoleRight;
import com.platform.exception.BizException;
import com.platform.ext.mybatis.annotation.UseDataSource;
import com.platform.service.IRight;

/**
 * 
 * <p> Title:Qx 实现</p>
 * <p> Description:  系统权限表</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Service

public class IRightImpl  implements IRight  {
	
	@Autowired
    private RightDAO<Right> DAO;
	@Autowired
	private RoleRightDAO<RoleRight> roleRightDAO;
        
    public RightDAO<Right> getDAO() {
        return DAO;
    }
    
    /** =========================== 数据库操作 ==================================== **/
    /**
     * 
     * selectAll(查询所有)
     * (这里描述这个方法适用条件 – 可选)
     * @return 
     * @return List
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_FIND")
    @UseDataSource()
    public List<Right> selectAll(Map<String,Object> map) {
        List<Right> s = null;
        try {
            s = DAO.selectByMap(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return s;
    }

    /**
     * 
     * selectTotal(查询总条数)
     * (这里描述这个方法适用条件 – 可选)
     * @param map
     * @return 
     * @return Integer
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_FIND")
    @UseDataSource()
    public Integer selectTotal(Map<String, Object> map) {
        Integer total = 0; 
        try {
            total = DAO.selectByMapCount(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return total;
    }

    /**
     * 
     * selectByPaging(分页查询)
     * (这里描述这个方法适用条件 – 可选)
     * @param pageUtil
     * @return 
     * @return List<Right>
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_FIND")
    @UseDataSource()
    public List<Right> selectByPaging(PageUtil pageUtil) {
        List<Right> pagingList = null;
        try {
            pagingList = DAO.selectByMapPaging(pageUtil);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return pagingList;
    }

    /**
     * 
     * delete(删除)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_DELETED")
    @UseDataSource()
    public boolean delete(Right dto) {
        boolean flag = false;
        try {
            if(DAO.deleteByPrimaryKey(dto) > 0) {
            	//删除权限信息，并删除对应的角色权限对照
            	if(roleRightDAO.deleteByRightId(dto.getId()) > 0) {
            		flag = true;
            	}
            }
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * update(修改)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_EDIT")
    @UseDataSource()
    public boolean update(Right dto) {
        boolean flag = false;
        try {
            if(DAO.updateBySelective(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * insert(新增)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="RIGHTMANAGER_SAVE")
    @UseDataSource()
    public boolean insert(Right dto) {
        boolean flag = false;
        try {
            if(DAO.insert(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    @Override
    @Authentication(requiredPre="RIGHTMANAGER_FIND")
    @UseDataSource
    public Right get(Object code) {
        Right dto = null;
        try {
            dto = DAO.selectByPrimaryKey(code);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return dto;
    }
    
    @Override
    @Authentication
    @UseDataSource
    public List<String> findSourceParent(String usercode){
    	return DAO.findSourceParent(usercode);
    }
    
    @Override
    @Authentication
    @UseDataSource
    public List<Right> findRightByUserCode(String usercode){
    	return DAO.findRightByUserCode(usercode);
    }

	@Override
	@Authentication(requiredPre="RIGHTMANAGER_FIND")
	@UseDataSource
	public List<String> findSourceByRole(String roleId) {
		return DAO.findSourceByRole(roleId);
	}
	
	@Override
	@Authentication
	@UseDataSource
	public void deleteBySourceCode(String sourceCode) {
		DAO.deleteBySourceCode(sourceCode);
	}
	
	@Override
	@Authentication
	@UseDataSource
	public List<Tree> authorizationModular(Map<?, ?> map) throws BizException {
		return this.DAO.authorizationModular(map);
	}
	@Override
	@Authentication
	@UseDataSource
	public List<Tree> queryRightByModular(Map<String, Object> map) throws BizException {
		return this.DAO.queryRightByModular(map);
	}
}